include("ersmodule.lua")
AddCSLuaFile("cl_admin.lua")
AddCSLuaFile("ersmodule.lua")

/*
	partyvan (1): anti-chat spam?
	partyvan: oh we also need votemaps on the admin mod
	partyvan: we need a way to ip ban people \"ers35: addip support?
*/

local ADMIN = {}
local gagged = {}

function ADMIN.ban(admin, cmd, args)
	local plyname 	= args[1]
	local time		= args[2] or 0 
	local reason	= args[3] or "You have been banned."
    
    local target = ers.GetPlayerByName(plyname)
	if target then
		target:Ban(time, reason)
	else
		admin:PrintMessage(HUD_PRINTCONSOLE, "Player not found.")
	end
end

function ADMIN.kick(admin, cmd, args)
	local plyname 	= args[1]
	local reason 	= args[2] or "You have been kicked."
	
	local target = ers.GetPlayerByName(plyname)
	if target then
		target:Kick(reason)
	else
		admin:PrintMessage(HUD_PRINTCONSOLE, "Player not found.")
	end
end

function ADMIN.noclip(admin, cmd, args)
	local plyname = args[1]
	
	local target = ers.GetPlayerByName(plyname)
	
	if target then
		if target:GetMoveType() != MOVETYPE_NOCLIP then
			target:SetMoveType(MOVETYPE_NOCLIP)
		else
			target:SetMoveType(MOVETYPE_WALK)
		end
	elseif !args[1] then
		if admin:GetMoveType() != MOVETYPE_NOCLIP then
			admin:SetMoveType(MOVETYPE_NOCLIP)
		else
			admin:SetMoveType(MOVETYPE_WALK)
		end
	else
		admin:PrintMessage(HUD_PRINTCONSOLE, "Player not found.") 
	end
end

function ADMIN.gag(admin, cmd, args)
	local plyname = args[1]
	local target = ers.GetPlayerByName(plyname)
	
	if gagged[target] then
		gagged[target] = nil
	elseif target then
		gagged[target] = target:Name()
	else
		admin:PrintMessage(HUD_PRINTCONSOLE, "Currently gagged:") 
		for ply, name in pairs(gagged) do
			admin:PrintMessage(HUD_PRINTCONSOLE, "\t" .. name) 
		end
	end
end

function ADMIN.goto(admin, cmd, args)
	local plyname = args[1]
	
	local target = ers.GetPlayerByName(plyname)
	if target then
		if target:Crouching() then //make sure we can fit
			admin:ConCommand("+duck")
		end
		admin:SetPos(target:GetPos())
	else
		admin:PrintMessage(HUD_PRINTCONSOLE, "Player not found.") 
	end
end

function ADMIN.bring(admin, cmd, args)
	local plyname = args[1]
	
	local target = ers.GetPlayerByName(plyname)
	if target then
		target:SetPos(admin:GetPos())
	else
		admin:PrintMessage(HUD_PRINTCONSOLE, "Player not found.") 
	end
end

function ADMIN.spec(admin, cmd, args)
	local plyname 	= args[1]
	local mode 		= args[2]
	
	local target = ers.GetPlayerByName(plyname)
	
	if mode == "1stperson" then
		admin:Spectate(OBS_MODE_IN_EYE)
		admin:SetMoveType(MOVETYPE_OBSERVER)
		admin:SpectateEntity(target)
	elseif mode == "3rdperson" then
		admin:Spectate(OBS_MODE_CHASE)
		admin:SetMoveType(MOVETYPE_OBSERVER)
		admin:SpectateEntity(target)
	elseif mode == "freeroam" then
		admin:Spectate(OBS_MODE_ROAMING)
		admin:SetMoveType(MOVETYPE_OBSERVER)
		admin:SpectateEntity(target)
	else
		admin:UnSpectate()
		admin:SetMoveType(MOVETYPE_WALK)
		admin:Spawn()
	end
end

local function CMDHandler(ply, cmd, args)
	local funcname = string.sub(cmd, 2)
	if ply:IsAdmin() then
		if ADMIN[funcname] then
			ADMIN[funcname](ply, cmd, args)
		end
	end
end

for cmd, func in pairs(ADMIN) do
	concommand.Add("_" .. cmd, CMDHandler)
end

//gamemode hooks
local function Gag(plylisten, plytalk)
	if gagged[plytalk] then
		print("gagged")
		return false
	else
		return true
	end
end
hook.Add("PlayerCanHearPlayersVoice", "gag", Gag)